﻿using Assets.BattleSystem.EnemyConfigs;
using Assets.Script.BattleSystem.Core;
using LiteNetLib.Utils;
using Script.NetSystem.Core;
using Script.NetSystem.Services.RoomAndBattle.BattleService;
using Script.Tools;
using Script.UiSystem.Room;
using UnityEngine;

namespace Script.UiSystem
{

    public class BattleController : Singleton<BattleController>
    {
        public BattleClient BattleClient { get; set; }

        public PollHandler PollHandler = () =>
        {
            
        };
        
        public BattleController()
        {
            
        }

        public void InitManagerBeforeSceneLoad()
        {
            //初始化创景
            GameObject go = new GameObject("BattleManager");
            go.AddComponent<BattleView>();
            
            //初始化角色
            BattleClient.BattleScenceLoadEnd((CaseSpawnConfig)GlobalCache.GetData("caseconfig"),
                RoomController.Instance.RoomView.GetPlayerRoleData());

            //通知服务器加载完毕
            LoadSuccess();
        }

        
        
        #region Client Send To Server
        
        public void SendChangeHp(float hp)
        {
            NetDataWriter msg = new NetDataWriter("changehp");
            msg.Put(hp);
            BattleClient.ClientSend(msg);
        }

        public void SendChangeMp(float mp)
        {
            NetDataWriter msg = new NetDataWriter("changemp");
            msg.Put(mp);
            BattleClient.ClientSend(msg);
        }
        
        public void SendAktStart(int index)
        {
            NetDataWriter msg = new NetDataWriter("upatks");
            msg.Put(index);
            BattleClient.ClientSend(msg);
        }

        public void SendAktEnd(int atkWay)
        {
            NetDataWriter msg = new NetDataWriter("upatke");
            msg.Put(atkWay);
            BattleClient.ClientSend(msg);
        }

        public void LoadSuccess()
        {
            NetDataWriter msg = new NetDataWriter("loadend");
            msg.Put(GlobalCache.PlayerNetId);
            BattleClient.ClientSend(msg);
        }

        public void SendAtkEnemy(int enemyId ,float damage)
        {
            NetDataWriter msg = new NetDataWriter("atkenemy");
            msg.Put(enemyId);
            msg.Put(damage);
            BattleClient.ClientSend(msg);
        }

        #endregion

    }
    
    public class BattleView : MonoBehaviour
    {
        
    }
}